package zhaomh.catgirl;

import org.bukkit.entity.Player;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class zNeko {
    private static zTools t = new zTools("&d[猫娘ねこむすめ]", null);
    private zNeko() {}
    public static boolean addNeko (Player p,boolean out) {
        if (out)
            t.setPlayer(p);

        List<String> list = getNekos();

        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).equals((t.getName()))) {
                if (out)
                    t.sendM2(zConfig.getLanguage("command.neko.catgirl.inherently"));
                return false;
            }
        }
        if (out)
            t.sendM2(zConfig.getLanguage("command.neko.catgirl.success"));
        zConfig.data.set("catgirls." + t.getName(), Map.of("owner","null","goodwill",0));
        zConfig.saveConfig();
        return true;

    }
    public static List<String> getNekos () {
        List<String> list = t.getKeysAtPath(zConfig.data,"catgirls");

        return list;
    }
    public static int getNekoNum () {
        int num = getNekos().size();

        return num;
    }
    public static boolean isNeko (Player p) {
        List<String> list = getNekos();
        String name = p.getName();

        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).equals(name)) {
                return true;
            }
        }

        return false;
    }
    public static boolean setGoodwill (Player p,int num) {

        if (isNeko(p))
            return false;

        zConfig.data.set("catgirls." + p.getName()+".goodwill",num);
        zConfig.saveConfig();
        return true;
    }
    public static boolean addGoodwill (Player p,int num) {

        if (isNeko(p) == false)
            return false;

        int num2 = zConfig.data.getInt("catgirls." + p.getName()+".goodwill");
        int sum = num + num2;

        zConfig.data.set("catgirls." + p.getName()+".goodwill",sum);
        zConfig.saveConfig();
        return true;

    }
    public static int getGoodwill (Player p,int def) {
        if (isNeko(p))
            return def;

        int num = zConfig.data.getInt("catgirls." + p.getName()+".goodwill");

        zConfig.saveConfig();
        return num;
    }
    public static List<String> getServants (Player p) {
        if (isOwner(p) == false)
            return null;

        List<String> list = zConfig.data.getStringList("owner."+p.getName());
        return list;
    }
    public static boolean addServants (Player p, Player p2) {
        List<String> list = new ArrayList<>();

        if (t.getKeysAtPath(zConfig.data,"owner").contains(p.getName())) {
            list = zConfig.data.getStringList("owner."+p.getName());
        }
        list.add(p2.getName());

        zConfig.data.set("owner."+p.getName(),list);
        zConfig.saveConfig();
        return true;
    }
    public static boolean isOwner (Player p) {
        if (zConfig.data.isList("owner." + p.getName()))
            return true;
        return false;
    }
    public static Player getOwner (Player p) {
        if (isNeko(p) == false)
            return null;

        String name = zConfig.data.getString("catgirls."+p.getName()+".owner");
        Player player = t.getNamePlayer(name);
        return player;
    }
    public static boolean haveOwner (Player p) {
        if (getOwner(p) == null || getOwner(p).getName().equals("null"))
            return false;

        return true;
    }
    public static boolean setOwner (Player p, Player p2) {
        if (isNeko(p) == false)
            return false;

        zConfig.data.set("catgirls."+p.getName()+".owner",p2.getName());
        zConfig.saveConfig();
        return true;
    }
    public static boolean NewOwner (Player p, Player p2, boolean out) {
        if (out)
            t.setPlayer(p);

        if (isNeko(p) == false) {
            if (out)
                t.sendM2(zConfig.getLanguage("command.neko.owner.no-catgirl"));

            return false;
        }

        if (haveOwner(p) == true) {
            if (out)
                t.sendM2(zConfig.getLanguage("command.neko.owner.have"));

            return false;
        }

        if (getOwner(p2) != null && getOwner(p2).getName().equals(p.getName())) {
            if (out)
                t.sendM2(zConfig.getLanguage("command.neko.owner.both"));
            return false;
        }

        setOwner(p,p2);
        t.sendAllServerMessage("add----"+addServants(p2,p));
        if (out) {
            String str = zConfig.getLanguage("command.neko.owner.success");
            str = str.replace("${owner}",p2.getName());
            t.sendM2(str);
        }
        return true;
    }
}
